package org.ibqs.provider.sys;

import java.util.List;
import java.util.Map;

import org.ibqs.core.base.BaseProviderImpl;
import org.ibqs.core.support.dubbo.spring.annotation.DubboService;
import org.ibqs.dao.sys.SysMenuMapper;
import org.ibqs.model.sys.SysMenu;
import org.ibqs.model.sys.ext.SysMenuBean;
import org.ibqs.provider.sys.ISysDicProvider;
import org.ibqs.provider.sys.ISysMenuProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;

import com.baomidou.mybatisplus.plugins.Page;

/**
 * @author ShenHuaJie
 * @version 2016年5月20日 下午3:19:19
 */
@CacheConfig(cacheNames = "sysMenu")
@DubboService(interfaceClass = ISysMenuProvider.class)
public class SysMenuProviderImpl extends BaseProviderImpl<SysMenu> implements ISysMenuProvider {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private ISysDicProvider sysDicProvider;

    public Page<SysMenu> query(Map<String, Object> params) {
        Page<String> idPage = this.getPage(params);
        idPage.setRecords(mapper.selectIdByMap(idPage, params));
        Page<SysMenu> pageInfo = getPage(idPage);
        Map<String, String> menuTypeMap = sysDicProvider.queryDicByDicIndexKey("MENUTYPE");
        for (SysMenu sysMenu : pageInfo.getRecords()) {
            if (sysMenu.getMenuType() != null) {
                sysMenu.setRemark(menuTypeMap.get(sysMenu.getMenuType().toString()));
            }
        }
        return pageInfo;
    }

    public Page<SysMenuBean> queryBean(Map<String, Object> params) {
        Page<String> idPage = this.getPage(params);
        idPage.setRecords(mapper.selectIdByMap(idPage, params));
        Page<SysMenuBean> pageInfo = getPage(idPage, SysMenuBean.class);
        Map<String, String> menuTypeMap = sysDicProvider.queryDicByDicIndexKey("MENUTYPE");
        for (SysMenuBean sysMenu : pageInfo.getRecords()) {
            if (sysMenu.getMenuType() != null) {
                sysMenu.setMenuTypeName(menuTypeMap.get(sysMenu.getMenuType().toString()));
            }
            if (sysMenu.getParentId() != null && !sysMenu.getParentId().equals("0")) {
                SysMenu parentMenu = sysMenuMapper.selectById(sysMenu.getParentId());
                sysMenu.setParentName(parentMenu.getMenuName());
            } else {
                sysMenu.setParentName("root");
            }
        }
        return pageInfo;
    }

    /* (non-Javadoc)
     * @see org.ibqs.provider.SysMenuProvider#getPermissions() */
    @Override
    public List<Map<String, String>> getPermissions() {
        return sysMenuMapper.getPermissions();
    }

}
